package com.sunjob.mroresumeproject.busactioncontroller;


import com.sunjob.common.domain.mroresume.CKBus;
import com.sunjob.common.domain.mroresume.RPBus;
import com.sunjob.common.domain.mroresume.TFBus;
import com.sunjob.common.mroAnno.ControllerLog;
import com.sunjob.common.mroAnno.LogModuleType;
import com.sunjob.common.result.Result;
import com.sunjob.common.util.MROUtils;
import com.sunjob.mroresumeproject.service.CKBusService;
import com.sunjob.mroresumeproject.service.RPBusService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/RP")
@CrossOrigin
@Api(tags = "机车维修测试接口")
public class RPBusController {
    @Autowired
    RPBusService rpBusService;


    //分页，上下页
    @RequestMapping("/getpages")
    public Result getpages(@RequestParam(value = "pages2")String pages2,String types){

        int page2=Integer.parseInt(pages2);

        if(page2<1){
            page2=1;
        }



        int size=5;
        //部件定检履历查出部件定检记录总条数
        System.out.println(types);
        int count=rpBusService.querycount(types);
        System.out.println(count);
        //求总页数
        int pageCount =((count-1)/size)+1;
        System.out.println(pageCount);
        if(page2>pageCount){
            page2=pageCount;
        }

//        //第一页放五条数据
//        int pages=(page2-1)*size;


        List<RPBus> list=rpBusService.queryrepair((page2-1)*size,size,types);

//        System.out.println(list);
        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);


        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;

    }


    //整车维修履历菜单查询
    @RequestMapping("/selectrepair")
    @ApiOperation(value = "整车维修分页查询")
    @ControllerLog(LogModuleType.RECORD)
    public Result selectrepair(@RequestParam(value ="types")String types){
        int page2=1;
        int size=5;

        //部件定检履历查出部件定检记录总条数
        int count=rpBusService.querycount(types);
        //求总页数
        int pageCount = count %size == 0 ? count / size : count / size + 1;



        //第一页放五条数据
        int pages=(page2-1)*size;
        List<RPBus> list=rpBusService.queryrepair(pages,size,types);

        //map存入不同类型对象
        Map map=new HashMap();
        map.put("page2",page2);
        map.put("count",count);
        map.put("pageCount",pageCount);
        map.put("list",list);

        //result对象返回数据到前端
        Result result=new Result();
        result.setData(map);
        return result;
    }

    //搜索框输入条件查询
    @RequestMapping("/selectRPcontent")
    @ApiOperation(value = "整车维修条件查询")
    public Result selectRPcontent(@ApiParam(value = "repairid",required = false) String repairid){
        //搜索框条件查询可以是部件定检编号或者部件名称
        List<RPBus> list=rpBusService.queryrepair2(repairid);

        //result返回查询出的部件定检履历记录
        Result result=new Result();
        result.setData(list);
        System.out.println(result);
        return result;


    }

    //新增按钮,机车维修新增
    @RequestMapping("/insertrepair")
    @ApiOperation(value = "整车维修履历新增")
    public void insertrepair(RPBus rpBus){

        DateTimeFormatter df = DateTimeFormatter.ofPattern("yyyy-MM-dd");
        LocalDateTime ldt = LocalDate.parse(rpBus.getStarttime2(),df).atStartOfDay();
        LocalDateTime ldt2 = LocalDate.parse(rpBus.getEndtime2(),df).atStartOfDay();
        if(rpBus.getFailreason()==""){

            rpBus.setFailreason("无");
            rpBus.setRPstate("完成");



        }
        else {
            rpBus.setRPstate("失败");
        }
        rpBus.setRepairid(MROUtils.nextId());
        rpBus.setStatus(1);
        rpBus.setStarttime(ldt);
        rpBus.setEndtime(ldt2);
        rpBusService.insertrepair(rpBus);



    }

    @RequestMapping("/insertRPBus")
    public void insertTFBus(RPBus rpBus) throws ParseException {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'");

        Date date = sdf.parse(rpBus.getStarttime2().split("\"")[1]);
//        System.out.println(date);
        Instant instant = date.toInstant();
        ZoneId zoneId = ZoneId.systemDefault();

        rpBus.setStarttime(instant.atZone(zoneId).toLocalDateTime());

        if (rpBus.getFailreason()==""){
            rpBus.setFailreason("无");
            rpBus.setRPstate("完成");
        }
        else {
            rpBus.setRPstate("失败");
        }
        Date date1 = new Date();
        Instant instant1 = date1.toInstant();
        ZoneId zoneId1 = ZoneId.systemDefault();

        rpBus.setEndtime(instant1.atZone(zoneId1).toLocalDateTime());
        rpBus.setStatus(1);

        System.out.println(rpBus);
        rpBusService.insertrepair(rpBus);
    }




    //编辑按钮，根据checkid查询出当前id的数据
    @RequestMapping("returnrepair")
    @ApiOperation(value = "整车维修履历插叙要编辑的数据")
    public Result returnrepair(@ApiParam(value = "repairid")Long repairid){
        List<RPBus>rpBuses=rpBusService.queryrepairone(repairid);
        Result result=new Result();
        result.setData(rpBuses);
        return result;
    }

    //对根据checkid查询出的部件定检履历数据做修改
    @RequestMapping("/updaterepair")
    @ApiOperation(value = "整车维修履历编辑")
    public void updaterepair(RPBus rpBus){

        rpBusService.updaterepair(rpBus);

    }

    //根据checkid删除修改这条记录的状态
    @RequestMapping("/deleterepair")
    @ApiOperation(value = "整车维修履历删除")
    public void deleterepair(@ApiParam("repairid")String repairid){
        //修改这条记录状态，0为消失，1为存在
        rpBusService.deleterepair(repairid);


    }

    @RequestMapping("/deleterepair2")
    public void deletetransform2(String repair2){
        System.out.println(repair2);
        String[] a=  repair2.split(",");
        String[] c=  a[0].split("\\[");
        System.out.println(c[0]);
        System.out.println(c.length);
        String[] d = a[a.length-1].split("\\]");
        for (int i = 0; i < a.length; i++) {
            if (i==0){
                a[0]=c[1];

            }
            if (i==a.length-1){
                a[a.length-1]=d[0];
            }
            rpBusService.deleterepairone(a[i]);
        }
    }

}
